﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for bzCity
/// </summary>
public class bzCity
{
    private dbConnection _DB = null;
    private string _TableName = "bzCity";
    private string _IDType = "_INT";
    private string _IDCol = "CityID";
    private string _TableChild = "bzDistrict";
    //kiem tra neu co parent thi doi method don - up

    public struct Enable
    {
        private static int _Enabled = 1;
        public static int Enabled { get { return _Enabled; } }

        private static int _Disabled = 1;
        public static int Disabled { get { return _Disabled; } }
    }
    public bzCity()
    {
        _DB = new dbConnection();
    }
    public DataTable getTable(string Key, string Enabled, string Page, string Limit)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@Page", Page);
        p[3] = new SqlParameter("@Limit", Limit);
        return _DB.getDataTable(_TableName + "_search", p);

    }
    public long getTotal(string Key, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[2];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        return _DB.getTotalTable(_TableName + "_getTotal", p);
    }
    public DataTable getRecord(string ID, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Enabled);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    }

    public int deleteRecord(string[] ID)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@TableChild", _TableChild);
            p[2] = new SqlParameter("@IDCol", _IDCol);
            p[3] = new SqlParameter("@IDValue", str);
            count += _DB.updateData("bz_deleteRecord" + _IDType, p);
        }
        return count;
    }
    public int activeRecord(string[] ID, string Enabled)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@Enabled", Enabled);
            count += _DB.updateData("bz_activeRecord" + _IDType, p);
        }
        return count;
    }

    public int saveOrder(string[] ID, string[] Order)
    {
        if (ID.Length != Order.Length) return 0;
        int count = 0;
        for (int i = 0; i < ID.Length; i++)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", ID[i]);
            p[3] = new SqlParameter("@Order", Order[i]);
            count += _DB.updateData("bz_saveOrder" + _IDType, p);
        }
        return count;

    }
    public int upOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);

        return _DB.updateData("bz_upOrder" + _IDType, p);
    }
    public int downOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        return _DB.updateData("bz_downOrder" + _IDType, p);
    }

    public int insertRecord(string Name, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[2];
        p[0] = new SqlParameter("@Name", Name);
        p[1] = new SqlParameter("@Enabled", Enabled);
        return _DB.updateData(_TableName + "_insert", p);
    }
    public DataTable getTableOther(string ID, string Page, string Limit)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@Page", Page);
        p[2] = new SqlParameter("@Limit", Limit);
        return _DB.getDataTable(_TableName + "_other", p);
    }
    public int updateRecord(string ID, string Name, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@Name", Name);
        p[2] = new SqlParameter("@Enabled", Enabled);
        return _DB.updateData(_TableName + "_update", p);
    }
}
